/*************************************************************************\
|    Authors:         Johanna Eberle, Alexandra Schmucker                 |
|    Date:            June 2017                                           |
|    SIAB version:    7517 v1                                             |
|    Purpose:         Generation of cross sectional data and              |
|                     biographical variables                              |
|    Do-file version: v0                                                  |
|                                                                         |
|    List of changes                                                      |
|    --------------------                                                 |
|    -                                                                    |
|                                                                         |
\*************************************************************************/

/*------------------------------------------------------------------------------------------------*\
  GENERATE CROSS-SECTIONS AT CUTOFF DATES
\*------------------------------------------------------------------------------------------------*/

cap log close
log using ${log}/02_${logname}_quer.log, replace

*-----------------------------*
* MAKE CROSS-SECTIONS         *
*-----------------------------*

* LOOP OVER SPECIFIED YEARS AND CUTOFF DATES
foreach y of numlist $years {
	foreach c of global cutoff {
		scalar cdate = date("`c'-`y'", "MDY")
		display _n(2) as result %td_CCYY-NN-DD cdate
		display _dup(157) "-"

		* USE PERIODS OVERLAPPING THE RESPECTIVE CUTOFF DATE
		use if begepi <= cdate & endepi >= cdate using ${data}/${dataname}_bio.dta, clear

		* NUMBER OF PARALLEL OBSERVATIONS PER CUTOFF DATE
		*tab level2 // uncomment to print table

		* NUMBER OF PARALLEL OBSERVATIONS OF A SOURCE PER CUTOFF DATE
		*tab level1 // uncomment to print table

		quietly { // Suppress display of output

		* INFORMATION ON A SECONDARY EMPLOYMENT (quelle == 1 & level1 ==1)
		* 'level1 == 1' from 'quelle == 1' is the second employment spell of an episode
		* Full-time secondary employment
		* Part-time secondary employment liable to social security
		* Marginal part-time secondary employment
		* secondary employment not specified

*#Alex: Problem wenn der 1. Beschäftigungsspell eine Sonderzahlung ist, müsste man den eigentlich ignorieren, oder?
		gen byte nb1 =  1 if teilzeit == 0                                   & level1 == 1
		replace  nb1 =  2 if teilzeit == 1                                   & level1 == 1
		replace  nb1 =  3 if inlist(erwstat, 109, 209)                       & level1 == 1
		replace  nb1 =  4 if quelle == 1 & (erwstat == .z | teilzeit == .z)  & level1 == 1 & nb1 == .
		replace  nb1 =  . if azubi == 1                                      & level1 == 1
		replace  nb1 =  . if grund != 54 									 & level1 == 1		//#Alex: Problem wenn es noch einen dritten Beschäftigungsspell gibt, müsste man den eigentlich heranziehen

		* INFORMATION ON PARALLEL OBSERVATIONS FROM OTHER SOURCES
		* 'level2 != 0' excludes main observation
		gen byte leh1 =  quelle ==  2                 & level2 != 0
		gen byte asu1 = (quelle ==  6 | quelle == 7)  & level2 != 0
		gen byte lhg1 =  quelle ==  3                 & level2 != 0
		gen byte mth1 = (quelle ==  4 | quelle == 5)  & level2 != 0

		* COPY INFORMATION ON PARALLEL OBSERVATIONS TO ALL OBSERVATIONS OF THE INDIVIDUAL
		bysort persnr: egen byte nb  = max(nb1)
		recode nb (.= .z) if quelle == 1
		recode nb (.= .n) if quelle != 1
		bysort persnr (leh1): gen byte leh = leh1[_N] if quelle != 2
		bysort persnr (asu1): gen byte asu = asu1[_N] if !inlist(quelle, 6, 7)
		bysort persnr (lhg1): gen byte lhg = lhg1[_N] if quelle != 3
		bysort persnr (mth1): gen byte mth = mth1[_N] if !inlist(quelle, 4, 5)
		recode leh asu lhg mth (.=.n)

		* DELETE AUXILIARY VARIABLES
		drop nb1 leh1 asu1 lhg1 mth1

		* SECONDARY EMPLOYMENT IS IN THE SAME ESTABLISHMENT
		sort persnr spell
		gen byte nb_betr = betnr == betnr[_n+1] if persnr == persnr[_n+1] & level2 ==0 & quelle ==1 & quelle[_n+1] ==1 & !missing(betnr)
		recode nb_betr (. = .n)
		replace nb_betr = .z if quelle == 1 & missing(betnr) & !missing(nb_betr)
		* Problem Sonderzahlungen: Die werden meistens nur für einen Monat gemeldet obwohl sie für den gesamten Beschäftigungszeitraum in einem Jahr gezahlt werden, daher ist das tengelt eigentlich falsch
		* TOTAL INCOME OF ALL JOBS AND BENEFIT RECEIPTS:
		bysort persnr: egen gtentgelt = total(tentgelt), missing
		recode gtentgelt (. = .z) if inlist(quelle, 1, 2)
		recode gtentgelt (. = .n) if !inlist(quelle, 1, 2)
		format gtentgelt %15.1f

		* KEEP MAIN OBSERVATION
		keep if level2 == 0

		* DATE VARIABLE OF CUTOFF
		gen int stichtag = cdate
		format stichtag %tdDD_Mon_CCYY

		* LABEL NEW VARIABLES
		label language en
		label variable nb         "Type of additional occupation"
		label variable nb_betr    "Additional occupation in same firm as main occupation"
		label variable leh        "Parallel observation: LeH"
		label variable asu        "Parallel observation: (X)ASU"
		label variable lhg        "Parallel observation: LHG"
		label variable mth        "Parallel observation: MTH"
		label variable nb_betr    "Secondary employment in same establishment"
		label variable gtentgelt  "Total income, all sources"
		label variable stichtag   "Cutoff date of the cross section"

		label define nb_en 1 "full-time job" ///
						   2 "part-time job" ///
						   3 "marginal part-time job" ///
						   4 "side job not specified" ///
						  .z "no entry" ///
						  .n "n/a"
		label values nb nb_en
		label define nb_betr_en  0 "other establishment" 1 "same establishment" .z "no entry" .n "n/a"
		label values nb_betr nb_betr_en
		label define gtentgelt_en .z "no entry" .n "n/a"
		label values gtentgelt gtentgelt_en

		label language de
		label variable nb         "Art der Nebenbeschaeftigung"
		label variable nb_betr    "Nebenbeschaeftigung im Betrieb der Hauptbeschaeftigung"
		label variable leh        "Gleichzeitige Beobachtung LeH"
		label variable asu        "Gleichzeitige Beobachtung (X)ASU"
		label variable lhg        "Gleichzeitige Beobachtung LHG"
		label variable mth        "Gleichzeitige Beobachtung MTH"
		label variable nb_betr    "Nebenbeschaeftigung im selben Betrieb"
		label variable gtentgelt  "Gesamteinkommen aus allen Quellen"
		label variable stichtag   "Stichtag des Querschnitts"

		label define nb_de 1 "Vollzeit-Job" ///
						   2 "Teilzeit-Job" ///
						   3 "geringfuegige Beschaeftigung" ///
						   4 "Nebenjob ohne naehere Angaben" ///
						  .z "k. Angabe" ///
						  .n "fehlt syst."
		label values nb nb_de
		label define nb_betr_de  0 "anderer Betrieb" 1 "selber Betrieb" .z "k. Angabe" .n "fehlt syst."
		label values nb_betr nb_betr_de
		label define gtentgelt_de .z "k. Angabe" .n "fehlt syst."
		label values gtentgelt gtentgelt_de

		numlabel, add mask("#  ")

		* COMPUTE DURATION VARIABLES AT CUTOFF DATE
		replace tage_erw = tage_erw - (endepi - cdate) if quelle ==1 & !(azubi ==1 & nb ==.z) // tage_erw disregards episodes of vocational training but considers secondary employment spells
		replace tage_bet = tage_bet - (endepi - cdate) if quelle ==1 & !mi(tage_bet)
		replace tage_job = tage_job - (endepi - cdate) if quelle ==1 & !mi(tage_job)
		replace tage_lst = tage_lst - (endepi - cdate) if  inlist(quelle, 2, 3)
		replace tage_lst = tage_lst - (endepi - cdate) if !inlist(quelle, 2, 3) & (leh ==1 | lhg ==1) // Adjust duration in case there is a benefit receipt parallel to the main status

		* DROP OBSOLETE VARIABLES
		drop begepi endepi spell level2 level1 azubi

		* SAVE CROSS-SECTION DATA SET
		compress
		save ${data}/${dataname}_quer_`y'_`c'.dta, replace


		} // End of quietly block

	} // End of loop over cutoff dates

} // End of loop over years

clear
log close
***CENSORED***exit












